package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.commons.exception.VenueRuntimeException;
import com.woniu.commons.exception.YogaException;
import com.woniu.commons.model.StateData;
import com.woniu.commons.util.DateTimeUtils;
import com.woniu.dao.YCoachEnterMapper;
import com.woniu.dao.YVenueBaseMapper;
import com.woniu.dao.YVenueCoachMapper;
import com.woniu.model.form.PageVenueBaseApplyForm;
import com.woniu.model.form.PageVenueBaseForm;
import com.woniu.model.model.YCoachEnter;
import com.woniu.model.model.YVenueBase;
import com.woniu.model.model.YVenueCoach;
import com.woniu.model.param.AddQualificationParam;
import com.woniu.model.param.UpdateVenueBaseLicenseParam;
import com.woniu.model.param.addVenueBaseTimeParam;
import com.woniu.service.YVenueBaseService;
import com.woniu.util.OSSUtil;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author EdisonLiu
 * @since 2024年05月23日
 */
@Service
public class YVenueBaseServiceImpl extends ServiceImpl<YVenueBaseMapper, YVenueBase> implements YVenueBaseService {
    @Resource
    private YVenueBaseMapper venueBaseMapper;
    @Resource
    private YVenueCoachMapper venueCoachMapper;







    @Override
    public YVenueBase loadVenueBaseById(Integer venueId) {

        YVenueBase venueBase = venueBaseMapper.selectOne(new QueryWrapper<YVenueBase>().eq("venue_id", venueId).eq("venue_state", StateData.VENUE_STATE_OK));

        return venueBase;
    }

    @Override
    public Integer updateVenueBaseById(YVenueBase venueBase) {
        YVenueBase vb = venueBaseMapper.selectOne(new QueryWrapper<YVenueBase>().eq("venue_id", venueBase.getVenueId()));
        if (vb == null){
            throw new VenueRuntimeException(3009,"该场馆详细不存在");
        }

        if (venueBase.getVenueName()!=null){
            vb.setVenueName(venueBase.getVenueName());
        }
        if (venueBase.getVenueDetails()!=null){
            vb.setVenueDetails(venueBase.getVenueDetails());
        }
        if (venueBase.getBusinessState()!=null){
            vb.setBusinessState(venueBase.getBusinessState());
        }
        if (venueBase.getVenueState()!=null){
            vb.setVenueState(venueBase.getVenueState());
        }
        if (venueBase.getVenueAddress()!=null){
            vb.setVenueAddress(venueBase.getVenueAddress());
        }
        if (venueBase.getBusinessStartTime()!=null){
            vb.setBusinessStartTime(venueBase.getBusinessStartTime());
        }
        if (venueBase.getBusinessEndTime()!=null){
            vb.setBusinessEndTime(venueBase.getBusinessEndTime());
        }
        if (venueBase.getLongitude()!=null){
            vb.setLongitude(venueBase.getLongitude());
        }
        if (venueBase.getLatitude()!=null){
            vb.setLatitude(venueBase.getLatitude());
        }
        if (venueBase.getArea()!=null){
            vb.setArea(venueBase.getArea());
        }
        if (venueBase.getCreateTime()!=null){
            vb.setCreateTime(venueBase.getCreateTime());
        }
        if (venueBase.getScore()!=null){
            vb.setScore(venueBase.getScore());
        }


        return venueBaseMapper.updateById(vb);
    }

    /**
     * 场馆更新认证图片地址
     * @param param 场馆许可的地址和场馆id
     * @return
     */
    @Override
    public Integer updateVenueBaseLicenseById(UpdateVenueBaseLicenseParam param) {
        YVenueBase venueBase = venueBaseMapper.selectOne(new QueryWrapper<YVenueBase>().eq("venue_id", param.getVenueId()).eq("venue_state", StateData.VENUE_STATE_OK));
        if (venueBase == null){
            throw new VenueRuntimeException(3010,"该id不存在场馆详情");
        }
        OSSUtil.deleteFile("edison-coco",venueBase.getVenueLicense());
        venueBase.setVenueLicense(param.getVenueLicense());
        int i = venueBaseMapper.updateById(venueBase);
        return i;
    }

    @Override
    public Integer addVenueBaseTime(addVenueBaseTimeParam param) throws YogaException {
        YVenueBase vb = venueBaseMapper.selectOne(new QueryWrapper<YVenueBase>().eq("venue_id", param.getVenueId()));
        LocalDateTime parse = LocalDateTime.parse(param.getBusinessStartTime(), DateTimeFormatter.ISO_DATE_TIME);
        vb.setBusinessStartTime(parse+"");

        LocalDateTime parse1 = LocalDateTime.parse(param.getBusinessEndTime(), DateTimeFormatter.ISO_DATE_TIME);
        vb.setBusinessEndTime(parse1+"");

        vb.setArea(param.getArea());

        LocalDateTime time = LocalDateTime.parse(param.getCreateTime(), DateTimeFormatter.ISO_DATE_TIME);
        vb.setCreateTime(time+"");

        vb.setClassroomNum(0);

        return venueBaseMapper.updateById(vb);
    }

    @Override
    public Page<YVenueBase> pageVenueBase(PageVenueBaseForm form) {
        form.setPageNum(form.getPageNum()==null?1: form.getPageNum());
        form.setPageSize(form.getPageSize()==null?5: form.getPageSize());

        Page<YVenueBase> page = new Page<>(form.getPageNum(), form.getPageSize());
        QueryWrapper<YVenueBase> wrapper = new QueryWrapper<>();
        if (form.getVenueState()!= null){
            wrapper.eq("venue_state",form.getVenueState());
        }
        if (form.getVenueName()!= null){
            wrapper.like("venue_name",form.getVenueName());
        }


        return baseMapper.selectPage(page, wrapper);
    }

    @Override
    public Page<YVenueBase> pageVenueBaseApply(PageVenueBaseApplyForm form) {
        form.setPageNum(form.getPageNum()==null?1: form.getPageNum());
        form.setPageSize(form.getPageSize()==null?5: form.getPageSize());

        Page<YVenueBase> page = new Page<>(form.getPageNum(), form.getPageSize());
        QueryWrapper<YVenueBase> wrapper = new QueryWrapper<>();
        if (form.getVenueName()!= null){
            wrapper.like("venue_name",form.getVenueName());
        }
        wrapper.eq("business_state",1);
        wrapper.eq("venue_state",1);

        List<YVenueCoach> venueCoaches = venueCoachMapper.selectList(new QueryWrapper<YVenueCoach>().eq("coach_id", form.getCoachId()).eq("coach_state", StateData.COACH_STATE_OK));
        if (venueCoaches.size()!=0){
            Set<Integer> collect = venueCoaches.stream().map(o -> o.getVenueId()).collect(Collectors.toSet());
            wrapper.notIn("venue_id",collect);
        }


        return baseMapper.selectPage(page, wrapper);
    }
}
